<template>
	<view class="container">
		<!-- 快递信息表单 -->
		<view class="form-box">
			<!-- 快递信息 -->
			<view class="express-section">
				<view class="form-item">
					<text class="label">快递位置</text>
					<picker mode="selector" :range="locations" @change="onLocationChange">
						<view class="picker">
							<text>{{formData.location || '请选择快递位置'}}</text>
							<uni-icons type="right" size="16" color="#999"></uni-icons>
						</view>
					</picker>
				</view>

				<view class="form-item">
					<text class="label">快递公司</text>
					<picker mode="selector" :range="companies" @change="onCompanyChange">
						<view class="picker">
							<text>{{formData.company || '请选择快递公司'}}</text>
							<uni-icons type="right" size="16" color="#999"></uni-icons>
						</view>
					</picker>
				</view>

				<view class="form-item">
					<text class="label">取件码</text>
					<input 
						type="text" 
						v-model="formData.pickupCode" 
						placeholder="请输入取件码" 
						maxlength="20"
					/>
				</view>

				<view class="form-item">
					<text class="label">备注信息</text>
					<textarea 
						v-model="formData.remark" 
						placeholder="请输入备注信息（如：具体位置、大小等）" 
						:maxlength="200"
					/>
					<text class="word-count">{{formData.remark.length}}/200</text>
				</view>
			</view>

			<!-- 配送信息 -->
			<view class="delivery-section">
				<view class="form-item">
					<text class="label">送达地点</text>
					<view class="location-select" @tap="selectDeliveryLocation">
						<text>{{formData.deliveryLocation || '请选择送达地点'}}</text>
						<uni-icons type="right" size="16" color="#999"></uni-icons>
					</view>
				</view>

				<view class="form-item">
					<text class="label">期望送达时间</text>
					<picker mode="time" :value="formData.expectedTime" @change="onTimeChange">
						<view class="picker">
							<text>{{formData.expectedTime || '请选择时间'}}</text>
							<uni-icons type="right" size="16" color="#999"></uni-icons>
						</view>
					</picker>
				</view>

				<view class="form-item">
					<text class="label">联系方式</text>
					<input 
						type="number" 
						v-model="formData.contact" 
						placeholder="请输入手机号码" 
						maxlength="11"
					/>
				</view>
			</view>

			<!-- 费用信息 -->
			<view class="price-section">
				<view class="price-item">
					<text class="label">基础配送费</text>
					<text class="price">¥{{deliveryFee}}</text>
				</view>
				<view class="price-item">
					<text class="label">加急服务</text>
					<switch :checked="formData.isUrgent" @change="onUrgentChange" color="#2979ff" />
				</view>
				<view class="price-item total">
					<text class="label">总计</text>
					<text class="price">¥{{totalPrice}}</text>
				</view>
			</view>
		</view>
		
		<!-- 发布按钮 -->
		<view class="submit-btn" @tap="submitOrder">
			发布订单
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			locations: ['菜鸟驿站', '中通快递', '京东快递', '顺丰快递', '邮政快递'],
			companies: ['中通快递', '圆通快递', '韵达快递', '申通快递', '顺丰快递', '京东快递', '邮政快递'],
			deliveryFee: 3,
			urgentFee: 5,
			formData: {
				location: '',
				company: '',
				pickupCode: '',
				remark: '',
				deliveryLocation: '',
				expectedTime: '',
				contact: '',
				isUrgent: false
			}
		}
	},
	computed: {
		totalPrice() {
			return (this.deliveryFee + (this.formData.isUrgent ? this.urgentFee : 0)).toFixed(2)
		}
	},
	methods: {
		onLocationChange(e) {
			this.formData.location = this.locations[e.detail.value]
		},
		onCompanyChange(e) {
			this.formData.company = this.companies[e.detail.value]
		},
		selectDeliveryLocation() {
			uni.showActionSheet({
				itemList: ['宿舍楼', '教学楼', '图书馆', '食堂'],
				success: (res) => {
					this.formData.deliveryLocation = ['宿舍楼', '教学楼', '图书馆', '食堂'][res.tapIndex]
				}
			})
		},
		onTimeChange(e) {
			this.formData.expectedTime = e.detail.value
		},
		onUrgentChange(e) {
			this.formData.isUrgent = e.detail.value
		},
		validateForm() {
			if (!this.formData.location) {
				uni.showToast({
					title: '请选择快递位置',
					icon: 'none'
				})
				return false
			}
			if (!this.formData.company) {
				uni.showToast({
					title: '请选择快递公司',
					icon: 'none'
				})
				return false
			}
			if (!this.formData.pickupCode) {
				uni.showToast({
					title: '请输入取件码',
					icon: 'none'
				})
				return false
			}
			if (!this.formData.deliveryLocation) {
				uni.showToast({
					title: '请选择送达地点',
					icon: 'none'
				})
				return false
			}
			if (!this.formData.expectedTime) {
				uni.showToast({
					title: '请选择期望送达时间',
					icon: 'none'
				})
				return false
			}
			if (!this.formData.contact) {
				uni.showToast({
					title: '请输入联系方式',
					icon: 'none'
				})
				return false
			}
			if (!/^1\d{10}$/.test(this.formData.contact)) {
				uni.showToast({
					title: '请输入正确的手机号',
					icon: 'none'
				})
				return false
			}
			return true
		},
		submitOrder() {
			if (!this.validateForm()) return

			// TODO: 发送请求保存订单
			uni.showLoading({
				title: '发布中...'
			})

			setTimeout(() => {
				uni.hideLoading()
				uni.showToast({
					title: '发布成功',
					icon: 'success'
				})
				// 返回首页
				uni.switchTab({
					url: '/pages/index/index'
				})
			}, 1500)
		}
	}
}
</script>

<style lang="scss">
.container {
	min-height: 100vh;
	background-color: $background-color;
	padding-bottom: 120rpx;
}

.form-box {
	background-color: #fff;
	padding: 20rpx;
	margin-top: 20rpx;
}

.express-section, .delivery-section {
	margin-bottom: 30rpx;
}

.form-item {
	margin-bottom: 30rpx;
	position: relative;

	.label {
		font-size: 28rpx;
		color: $text-color;
		margin-bottom: 20rpx;
		display: block;
	}

	input, .picker, .location-select {
		width: 100%;
		background-color: #f5f5f5;
		padding: 20rpx;
		border-radius: 8rpx;
		font-size: 28rpx;
		color: $text-color;
	}

	.picker, .location-select {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	textarea {
		width: 100%;
		height: 160rpx;
		background-color: #f5f5f5;
		padding: 20rpx;
		border-radius: 8rpx;
		font-size: 28rpx;
	}

	.word-count {
		position: absolute;
		right: 20rpx;
		bottom: 20rpx;
		font-size: 24rpx;
		color: #999;
	}
}

.price-section {
	background-color: #fff;
	padding: 20rpx;
	margin-top: 20rpx;

	.price-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20rpx;

		.label {
			font-size: 28rpx;
			color: $text-color;
		}

		.price {
			font-size: 32rpx;
			color: #ff5500;
			font-weight: bold;
		}

		&.total {
			margin-top: 30rpx;
			padding-top: 20rpx;
			border-top: 1rpx solid $border-color;
		}
	}
}

.submit-btn {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	height: 100rpx;
	background-color: $theme-color;
	color: #fff;
	font-size: 32rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}
</style> 